package accountinfo.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import accountinfo.dao.AccountDetailsDAO;
import accountinfo.dao.AccountDetail;

public class AccountDetails extends ServletPojoAdapter {

	private static final long serialVersionUID = 1L;

	protected Object getBusinessMethod(Map<String, String[]> params) {
		// Query Parameters
		final String cardNO = params.get("cardNO")[0];
		final String cardsIN = params.get("cardsIN")[0];

		// Default response
		final Map<String, Object> ret = new HashMap<String, Object>();
		final List<Map<String, Object>> accounts = new LinkedList<Map<String, Object>>();
		ret.put("accounts", accounts);
		ret.put("status", "Card Not Found");

		
		String cardSearch = cardNO;
		// Get Account Number (11 digits) if requestor wants all cards)
		if( cardsIN == "ALL" ){cardSearch = cardNO.substring(0, 11);}
		
		final List<AccountDetail> items = AccountDetailsDAO.getAccountDetails(cardSearch);
						
		if (!items.isEmpty())
		{
			for (AccountDetail item : items) {
				final Map<String, Object> account = new HashMap<String, Object>();
				account.put("cardNO", item.getCardNO());
				account.put("lastStatementDT", item.getLastStatementDT());
				account.put("titleDS", item.getTitleDS());
				account.put("firstNM", item.getFirstNM());
				account.put("lastNM", item.getLastNM());
				account.put("basicCardIN", item.getBasicCardIN());
				account.put("cardSinceDT", item.getCardSinceDT());
				account.put("emailAddress", item.getEmailAddress());
				account.put("birthDT", item.getBirthDT());
				account.put("memberSinceDT", item.getMemberSinceDT());
				account.put("vipStatusIN", item.getVipStatusIN());
				account.put("address", item.getAddressMap());
				accounts.add(account);
			}
			ret.put("status", "Success");
		}		
		return ret;
	}
}
